import count from '@dfeidao/atom-nodejs/db/count';
import query from '@dfeidao/atom-nodejs/db/query';
import { IncomingHttpHeaders } from 'http';
import { productid, systemid } from '../../atom/config';
interface Message {
	filter: {
		responsible_no: string,
		equip_depart: string,
		project: string,
		assetsname: string,
		asset_classification: string,
		timeout_alert: string,
		customer_assets: string,
		type: string,
		name: string,
		pn: number
	};
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders) {
	// 获取数据总条数
	const c = count(action_id, session_id, systemid);
	c.prepare('project_assets', {
		productid, responsible_no: { $like: message.filter.responsible_no },
		equip_depart_no: { $like: ("%" + message.filter.equip_depart + "%") },
		project_no: { $like: ("%" + message.filter.project + "%") },
		asset_classification_no: { $like: ("%" + message.filter.asset_classification + "%") },
		assetsname: { $like: ("%" + message.filter.assetsname + "%") },
		timeout_alert: { $like: ("%" + message.filter.timeout_alert + "%") },
		customer_assets: { $like: ("%" + message.filter.customer_assets + "%") }
	});
	const [coun] = await c.exec();
	// 获取数据
	const cq = query(action_id, session_id, systemid);
	if (message.filter.type === "0") {
		// 正序
		cq.prepare('project_assets', ['asset_classification_no', 'asset_classification_name',
			'assetsname', 'project_name', 'equip_depart_no', 'place', 'project_asset_no', 'equip_depart_name', 'asset_grade', 'timeout_alert', 'customer_assets', 'maintenance_time'],
			{
				productid, responsible_no: { $like: message.filter.responsible_no },
				equip_depart_no: { $like: ("%" + message.filter.equip_depart + "%") },
				project_no: { $like: ("%" + message.filter.project + "%") },
				asset_classification_no: { $like: ("%" + message.filter.asset_classification + "%") },
				assetsname: { $like: ("%" + message.filter.assetsname + "%") },
				timeout_alert: { $like: ("%" + message.filter.timeout_alert + "%") },
				customer_assets: { $like: ("%" + message.filter.customer_assets + "%") }
			}, 20, message.filter.pn, [message.filter.name], []);
		const [dt] = await cq.exec();
		return { dt, coun };
	} else {
		// 倒序
		cq.prepare('project_assets', ['asset_classification_no', 'asset_classification_name',
			'assetsname', 'project_name', 'equip_depart_no', 'place', 'project_asset_no', 'equip_depart_name', 'asset_grade', 'timeout_alert', 'customer_assets', 'maintenance_time'],
			{
				productid, responsible_no: { $like: message.filter.responsible_no },
				equip_depart_no: { $like: ("%" + message.filter.equip_depart + "%") },
				project_no: { $like: ("%" + message.filter.project + "%") },
				asset_classification_no: { $like: ("%" + message.filter.asset_classification + "%") },
				assetsname: { $like: ("%" + message.filter.assetsname + "%") },
				timeout_alert: { $like: ("%" + message.filter.timeout_alert + "%") },
				customer_assets: { $like: ("%" + message.filter.customer_assets + "%") }
			}, 20, message.filter.pn, [], [message.filter.name]);
		const [dt] = await cq.exec();
		return { dt, coun };
	}

}
